<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>snsapi_base</title>
<script type="text/javascript" src="./js/jquery.min.js"></script>

</head>
<body>
本页面用于隐式授权测试<br><br>
<div id="a">

</div>


<script type="text/javascript">
var appid="wxc408c0125834275f";
var url=encodeURIComponent(location.href.split("#")[0]);
var scope="snsapi_base"; //snsapi_userinfo 
var param=getParams(location.search.substr(1));
function getParams(params){
	var list=params.split("&");
	var result={};
	for(index in list){
		var kv=list[index].split("=");
		result[kv[0]]=kv[1];
	}
	return result;
}

document.ready = function () {
	if(!param["code"]){
		var codeUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+url+"&response_type=code&scope="+scope+"&state=STATE#wechat_redirect";
		location.href=codeUrl;
	}
	else{
		$("#a").append("code:"+param["code"]+"<br>");
		var requestUrl="wx/getOpenid.zx?code="+param["code"];
		loadXMLDoc("GET",requestUrl,true);
		console.log("code:"+param["code"]);
	}
}
document.ready();

var xmlhttp;
//type:POST OR GET ,Async true:同步方法,false:异步方法
function loadXMLDoc(type,url,Async) {
	xmlhttp = null;
	if (window.XMLHttpRequest) {// code for all new browsers
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE5 and IE6
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp != null) {
		xmlhttp.onreadystatechange = state_Change;
		xmlhttp.open("GET", url, Async);
		xmlhttp.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change() {
	if (xmlhttp.readyState == 4) {// 4 = "loaded"
		if (xmlhttp.status == 200) {// 200 = OK
			var data=JSON.parse(xmlhttp.responseText);
			var access_token=data.access_token;
			var expires_in=data.expires_in;
			var openid=data.openid;
			var refresh_token=data.refresh_token;
			var scope=data.scope;
			for(key in data){
				$("#a").append(key+":"+data[key]+"<br>");
			}
			
		} else {
			alert("Problem retrieving XML data");
		}
	}
}
</script>
</body>
</html>